<?php

class SearchController extends FrontEndController {

    public function filters() {
        return array(
            'accessControl',
            'postOnly + delete'
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     *
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array(
                'allow',
                'actions' => array(
                    'index',
                    'tag',
                    'brand'
                ),
                'users' => array(
                    '*'
                )
            ),
            array(
                'deny',
                'users' => array(
                    '*'
                )
            )
        );
    }

    /**
     * Declares class-based actions.
     */
    public function actions() {
        return array(
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF
            ),
            'page' => array(
                'class' => 'CViewAction'
            )
        );
    }

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionIndex() {
        $model = Image::model()->findAllBySql("SELECT *,view_cnt+(like_cnt*5)as 'hot' FROM tbl_image ORDER BY hot DESC LIMIT 0,10");
        $this->render('index', array(
            'model' => $model
        ));
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError() {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error ['message'];
            else
                $this->render('error', $error);
        }
    }

    /**
     * Displays the contact page
     */
    public function actionTag($item_name) {
        $item = Item::model()->find('name=:name and status=:status', array(':name' => $item_name, ':status' => Item::ITEM_ACTIVE));
        if (!$item) {
            echo 'not found!';
            exit();
        }

        $model = ImageTag::model()->findAll('item_id=:item_id and status=:status', array(':item_id' => $item->id, ':status' => ImageTag::TAG_ACTIVE));
        if ($model) {
            foreach ($model as $tag) {
                echo'<b>Item:</b> ' . $tag->item->name . '<br/>';
                echo'<b>Thương hiệu:</b> ' . $tag->brand . '<br/>';
                echo'<b>Image_URL:</b> ' . $tag->image->img_url . '<hr/>';
            }
        } else {
            echo 'not found!';
        }
        exit();

        if (isset($_POST ['ContactForm'])) {
            $model->attributes = $_POST ['ContactForm'];
            if ($model->validate()) {
                $name = '=?UTF-8?B?' . base64_encode($model->name) . '?=';
                $subject = '=?UTF-8?B?' . base64_encode($model->subject) . '?=';
                $headers = "From: $name <{$model->email}>\r\n" . "Reply-To: {$model->email}\r\n" . "MIME-Version: 1.0\r\n" . "Content-type: text/plain; charset=UTF-8";

                mail(Yii::app()->params ['adminEmail'], $subject, $model->body, $headers);
                Yii::app()->user->setFlash('contact', 'Thank you for contacting us. We will respond to you as soon as possible.');
                $this->refresh();
            }
        }
        $this->render('contact', array(
            'model' => $model
        ));
    }

    public function actionBrand($item_name, $brand) {
        $item = Item::model()->find('name=:name and status=:status', array(':name' => $item_name, ':status' => Item::ITEM_ACTIVE));
        if (!$item) {
            echo 'not found!';
            exit();
        } else {

            $model = ImageTag::model()->findAll('item_id=:item_id and brand=:brand and status=:status', array(':item_id' => $item->id, ':brand' => $brand, ':status' => ImageTag::TAG_ACTIVE));
            if ($model) {
                foreach ($model as $tag) {
                    echo'<b>Item:</b> ' . $tag->item->name . '<br/>';
                    echo'<b>Thương hiệu:</b> ' . $tag->brand . '<br/>';
                    echo'<b>Image_URL:</b> ' . $tag->image->img_url . '<hr/>';
                }
            } else {
                echo 'not found!';
            }
            exit();
        }
    }

}